<?php

class Core {
		
	public $db;
	public $tpl;	
	
	public function __construct()
	{
		$this->db = Database::instance();
		$this->tpl = Template::instance();
	}
	
	public function installmentBank()
	{
		$result = $this->db->selectAll("SELECT * FROM bank");
		
		return $result;
	}
	
	public function installmentInfo()
	{
		$id = (int)$_GET['id'];
		$result = $this->db->selectOne("SELECT * FROM installment WHERE id=$id");	
		
		
		$a = 1;
		$payment = array();
		foreach(explode('|', $result->payment) as $i)
		{
			$cost = ($result->months-$a)*$result->cost;	
			
			$payment[] = array(($a++), $i, $cost);
		}
		
		$this->tpl->assign('payment', $payment);
		$this->tpl->display("installment-info.tpl");	
	}
	
	public function installmentList()
	{
		
		$this->tpl->assign('listViews', $this->db->selectAll("SELECT i.*, (i.months*i.cost) as costs FROM installment i"));
		$this->tpl->display("installment.tpl");	
	}	
	
	public function installmentNew()
	{
		$this->tpl->assign('bank', $this->installmentBank());	
		$this->tpl->display("installment-form.tpl");	
	}	
	
	public function installmentEdit()
	{
		$id = (int)$_GET['id'];
		$this->tpl->assign('bank', $this->installmentBank());
		$this->tpl->assign('value', $this->db->selectOne("SELECT * FROM installment WHERE id=$id"));
		$this->tpl->display("installment-form.tpl");	
	}	
}

?>